var validator;
var $roleAddForm = $("#daily-add-form");

//添加日报
//(资源)行数默认1行 标记
var rowCount = 0;

//日报数据
var rewardsJsonData;

//效率数
var sum;

function addCenterIpGrp(obj) {
    rowCount++
    html =
        '<div class="row text-left"  id="add_input' + rowCount + '">' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<span class="input-group-addon">项目代码：</span>' +
        '<div class="form-group">' +

        '<select multiple="multiple"  id="projectNoSelect' + rowCount + '"></select>' +
        '<input type="hidden"   id="projectNo_add' + rowCount + '" placeholder="请输入项目代码">' +
        '</div></div></div>' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<span class="input-group-addon">测试类型：</span>' +
        '<div class="form-group">' +
        '<select class="form-control" id="articleType_add' + rowCount + '">' +
        '<option value="" selected >请选择测试类型</option>' +
        '<option value="用例执行">用例执行</option>' +
        '<option value="专项执行">专项执行</option>' +
        '<option value="探索测试">探索测试</option>' +
        '<option value="缺陷/修改点验证、压测">缺陷/修改点验证、压测</option>' +
        '<option value="培训学习">培训学习</option>' +
        '<option value="组长事务">组长事务</option>' +
        '</select>' +
        '</div></div></div>' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<span class="input-group-addon">用例条数：</span>' +
        '<div class="form-group">' +
        '<input type="text" onkeyup="value=value.replace(/[^\\d.]/g,\'\')"   class="form-control text-info" id="articleNumber_add' + rowCount + '" placeholder="请输入项目测试用例条数">' +
        '</div></div></div>' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<label  class="input-group-addon">计划投入工时：</label>' +
        '<div class="form-group">' +
        '<input type="text" onkeyup="value=value.replace(/[^\\d.]/g,\'\')" class="form-control text-info" id="plannedLaborHours_add' + rowCount + '" placeholder="请输入计划投入工时">' +
        '</div></div></div>' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<label class="input-group-addon ">实际投入工时：</label>' +
        '<div class="form-group">' +
        '<input type="text" onkeyup="value=value.replace(/[^\\d.]/g,\'\')" class="form-control text-info" id="actuaLinputHours_add' + rowCount + '" placeholder="请输入实际投入工时">' +
        '</div></div></div>' +

        '<div class="col-sm-11" >' +
        '<div class="input-group">' +
        '<label class="input-group-addon ">投入内容：</label>' +
        '<div class="form-group">' +
        '<input type="text" class="form-control text-info " id="inputContent_add' + rowCount + '" placeholder="请输入投入内容">' +
        '</div></div></div>' +


        '<span class="input-group-btn">' +
        '<button class="btn btn-save" type="button" onclick="deleteInput()"   id="delCenterIpGrp">删除</button>' +
        '</span>' +

        '<div class="row" >' +
        '<span>------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------' +
        '</span>' +
        '</div>' +
        '</div>'

    obj.insertAdjacentHTML('beforebegin', html);
    initProjectNoShow(rowCount)
    /* for (i=1; i<rowCount; i++){
           $("#delCenterIpGrp"+rowCount+"").attr("disabled","true");
       }
       $("#delCenterIpGrp"+rowCount+"").attr("disabled","false");*/

}


$(function () {
    /**
     * 动态删除日报
     */
    /*    $(document).on('click', '#delCenterIpGrp', function () {
            //这中可以的问题有点多
            //rowCount--;
            var el = this.parentNode.parentNode
            var centerIp = $(this).parent().parent().find('#ipInput').val();
            el.parentNode.removeChild(el)
        })*/

    /**
     * 按钮监听
     */
    $("#daily-add .btn-save").click(function () {
        var name = $(this).attr("name");
        var validator = $roleAddForm.validate();
        var flag = validator.form();
        var rewardArray = new Array();
        if (flag) {
            //下一步按钮
            if (name === "save") {
                //取到动态输入的值
                for (var i = 1; i <= rowCount; i++) {
                    //项目代码
                    var projectNo = $('div .input-group #projectNo_add' + i).val();
                    //计划投入工时
                    var plannedLaborHours = $('div .input-group #plannedLaborHours_add' + i).val();
                    //实际投入工时
                    var actuaLinputHours = $('div .input-group #actuaLinputHours_add' + i).val();
                    //投入内容
                    var inputContent = $('div .input-group #inputContent_add' + i).val();
                    //测试用例条数
                    var articleNumber = $('div .input-group #articleNumber_add' + i).val();
                    //测试类型
                    var articleTyep = $('div .input-group #articleType_add' + i).val();
                    //判断有没有填
                    if (
                        //项目代码
                        (projectNo == "" || projectNo == undefined) ||
                        //计划投入工时
                        (plannedLaborHours == "" || undefined == plannedLaborHours) ||
                        //实际投入工时
                        (actuaLinputHours == "" || undefined == actuaLinputHours) ||
                        //投入内容
                        (inputContent == "" || undefined == inputContent) ||
                        //测试用例条数
                        (articleNumber == "" || undefined == articleNumber) ||
                        //测试类型
                        (articleTyep == "" || undefined == articleTyep)) {
                        $MB.n_danger("亲！六项都为必填哦");
                        rewardArray = [];
                        break
                    } else {
                        if (
                            //项目代码
                            (projectNo != "" && projectNo != undefined) &&
                            //计划投入工时
                            (plannedLaborHours != "" && undefined != plannedLaborHours) &&
                            //实际投入工时
                            (actuaLinputHours != "" && undefined != actuaLinputHours) &&
                            //投入内容
                            (inputContent != "" && undefined != inputContent) &&
                            //测试用例条数
                            (articleNumber != "" && undefined != articleNumber) &&
                            //测试类型
                            (articleTyep != "" && undefined != articleTyep)) {
                            var obj = new Object();
                            //项目代码
                            obj['projectNo'] = projectNo;
                            //计划投入工时
                            obj['plannedLaborHours'] = plannedLaborHours;
                            //实际投入工时
                            obj['actuaLinputHours'] = actuaLinputHours;
                            //投入内容
                            obj['inputContent'] = inputContent;
                            //测试用例条数
                            obj['articleNumber'] = articleNumber;
                            //测试类型
                            obj['articleType'] = articleTyep;
                            rewardArray.push(obj);
                        }
                    }

                }

                if (rewardArray.length > 0) {
                    //计划
                    var plannedLaborHoursNum = 0;
                    //实际投入工时
                    var actuaLinputHoursNum = 0;
                    rewardArray.forEach(obj => {
                        plannedLaborHoursNum += parseFloat(obj.plannedLaborHours);
                        actuaLinputHoursNum += parseFloat(obj.actuaLinputHours);
                    });
                    //当日的总计划 /实际投入
                    sum = (plannedLaborHoursNum / actuaLinputHoursNum) * 100;
                    //效率
                    var executionefFiciency = parseInt(sum).toString() + "%";
                    //效率赋值
                    $("#executioneFficiency").val(executionefFiciency);
                    //打开模态框
                    $('#myModal').modal('show')
                    //日报数据
                    rewardsJsonData = JSON.stringify(rewardArray);
                    rewardArray = [];
                }

                //关闭
            } else if (name === "close_add") {
                //关闭页面
                $('#daily-add').modal('hide');
                //关闭所有inpu的
                deleteInput()
            }

        }
    });

    /*    //第一种：当模态框完全关闭时的初始化
        $('#daily-add').on('hide.bs.modal', function () {
            $('input').removeAttr("readonly");
        })*/

    //第二种：当模态框完全关闭时的初始化
    $('#daily-add').on('hidden.bs.modal', function (e) {
        //第一种：form表单清空
        $("#daily-add-form")[0].reset();
    });

    //第二种：当模态框完全关闭时的初始化
    $('#myModal').on('hidden.bs.modal', function (e) {
        //第一种：form表单清空
        $("#form-horizontal")[0].reset();
    });

    //下一步的窗口 提交触发
    $("#myModal .btn-save").click(function () {
        var name = $(this).attr("name");
        //提交
        if (name === "present_add") {
            //点击按钮屏蔽一次防止多次触发
            $("#present_add").attr("disabled", true);
            //执行效率
            var executioneFficiency = $("#executioneFficiency").val();
            //执行效率原因
            var reasonsforeFficiencyFailure = $("#reasonsforeFficiencyFailure").val();
            //执行效率低于百分之90 原因为必填项
            if (sum < 90) {
                //不为空
                if (reasonsforeFficiencyFailure == null || reasonsforeFficiencyFailure == "") {
                    $MB.n_danger("亲！ 效率不代表 原因为必填项哦");
                    //点击按钮屏蔽一次防止多次触发
                    $("#present_add").attr("disabled", false);
                    return;
                }
            }

            $.post(ctx + "daily/present", {
                "daily": rewardsJsonData,
                "executioneFficiency": executioneFficiency,
                "reasonsforeFficiencyFailure": reasonsforeFficiencyFailure
            }, function (r) {

                if (r.code === 0) {
                    //关闭效率页面
                    $('#myModal').modal('hide');
                    //关闭日报添加页面
                    $('#daily-add').modal('hide');
                    //关闭所有inpu的
                    //关闭所有inpu的
                    deleteInput()
                    //点击按钮屏蔽一次防止多次触发
                    $("#present_add").attr("disabled", false);
                    $MB.n_success(r.msg);
                } else {
                    //点击按钮屏蔽一次防止多次触发
                    $("#present_add").attr("disabled", false);
                    $MB.n_danger(r.msg);
                }
            });
        }
    });

});

/**
 * 删除
 */
function deleteInput() {
    //关闭所有inpu的
    for (let i = 0; i < 20; i++) {
        //关闭所有inpu的
        var el = document.getElementById("add_input" + i + "");
        el == null ? "" : el.remove();
    }
    rowCount = 0;
}


/**
 * 判断当天是否添加日报
 */
function ifDaliy(obj) {

    $.ajax({
        url: "dailyShow/ifDaliy",
        type: "post",
        processData: false,
        contentType: false,
        success: function (res) {
            if (res) {
                //数据的效验 false 为表示没有数据 true 表示今天添加了数据
                if (res.msg) {
                    $MB.n_danger("你的当天日报已经添加如果想要继续添加请到日报展示删除当天的日报再来操作");
                } else {
                    //添加日报
                    addCenterIpGrp(obj)
                }
            }

        },
        error: function (err) {
            $MB.n_danger("网络连接失败,稍后重试", err);
        }

    })
}

/**
 * 动态下拉款
 * @param obj
 */
function initProjectNoShow(obj) {
    $.post(ctx + "projectNum/list", {}, function (r) {
        var data = r.rows;
        var option = "";
        for (var i = 0; i < data.length; i++) {
            option += "<option value='" + data[i].projectNum + "'>" + data[i].projectNum + "</option>"
        }
        //动态拼接
        $('#projectNoSelect' + obj).html("").append(option);
        var options = {
            placeholder: "请选择",
            allSelected: '选择全部',
            //是否有全显
            selectAll: false,
            single: false,
            width: '100%',
            onClose: function () {
                //回显
                $('#projectNo_add' + obj).val($('#projectNoSelect' + obj).val());
            }
        };
        $('#projectNoSelect' + obj).multipleSelect(options);
    });
}
